package com.zfzs.post.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 数据校验
 *
 * @author: ck
 * @create: 2019-09-29 14:05
 **/
public class CheckData {
    /**
     * 从列表中找出包括Value值的对象列表
     *
     * @param list
     * @param methodName
     * @param value
     * @return
     */
    public static <T, V> List<T> findObjFromList(List<T> list, String methodName, V value) {

        Predicate<T> predicate = new Predicate<T>() {

            @Override
            public boolean evaluate(T obj) {
                // TODO Auto-generated method stub
                try {
                    Method method = obj.getClass().getMethod(methodName);
                    @SuppressWarnings("unchecked")
                    V keyName = (V) method.invoke(obj);
                    return keyName.equals(value);
                } catch (Exception e) {
                    return false;
                }
            }
        };

        List<T> result = (List<T>) CollectionUtils.select(list, predicate);
        return result;

    }
}
